//
//  HMStatusOriginalView.swift
//  大深圳微博01
//
//  Created by shenzhenIOS on 16/4/14.
//  Copyright © 2016年 shenzhenIOS. All rights reserved.
//

import UIKit

/// 微博原创view
class HMStatusOriginalView: UIView {
    
    // 微博模型
    var status: HMStatus? {
        didSet {
            // cell将模型设置给微博原创view后,微博原创view自己来设置自己的内容
            // 设置头像
            if let profile_image_url = status?.user?.profile_image_url {
                // 有图片地址
                let url = NSURL(string: profile_image_url)!
//                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
                
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"), completed: { (image, error, _, _) -> Void in
                    if error != nil {
                        HMPrint("设置头像出错了: \(error)")
                        return
                    }
                    
//                    // 将图片异步绘制成圆角的
                    image.hm_AsyncDrawImage(nil, isCorner: true, drawFinish: { (image) -> () in
                        self.iconView.image = image
                    })
                })
            }
            
            // 用户名称
            nameLabel.text = status!.user!.screen_name! + ", \(status!.user!.mbrank)"
            
            // 时间
            // 每次显示的时候都去重新计算这个时间
            timeLabel.text = NSDate.date(status?.created_at ?? "刚刚").sinaDateDescription()
            
            // 来源
            sourceLabel.text = status?.source
            
            // 微博内容
            contentLabel.text = status?.text
            
            // 微博模型提供的数据不完整,view不能直接拿来用,还需要处理,如果多个地方要使用,这段代码需要拷贝到多个地方,当cell不停滚动的时候,是经常需要计算
            // 让模型直接提供会员等级和认证类型对应的图片
            // 设置用户的会员等级
            
//            memberView.image = status?.user?.mbrankImage
//            status?.user?.mbrankImage?.hm_AsyncDrawImage(nil, drawFinish: { (image) -> () in
//                self.memberView.image = image
//            })

            // 有图片,会员等级不是0
            if let mbrankImage = status?.user?.mbrankImage {
                mbrankImage.hm_AsyncDrawImage(nil, drawFinish: { (image) -> () in
                    self.memberView.image = image
                })
            } else {
                self.memberView.image = nil
            }
            
            // 认证类型
            verifiedView.image = status?.user?.verifiedImage
//            status?.user?.verifiedImage?.hm_AsyncDrawImage(nil, isCorner: true, drawFinish: { (image) -> () in
//                self.verifiedView.image = image
//            })
            
            // 将微博模型的图片url设置给HMStatusPictureView,HMStatusPictureView会自己去计算宽高
            picutreView.pictureURLs = status?.storePicutreURLs
            
            // 当没有图片的时候,HMStatusPictureView距离contentLabel的底部不需要8的间距
            
            // snp_updateConstraints: 前提是参照的view和参照的约束没有变化才能updateConstraints
            picutreView.snp_updateConstraints { (make) -> Void in
                let margin = status?.storePicutreURLs == nil ? 0 : HMStatusCellMargin
                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
        
        prepareUI()
    }
    
    private func prepareUI() {
        // 1.添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        addSubview(picutreView)
        
        // 2.添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(HMStatusCellMargin)
            make.size.equalTo(CGSize(width: HMStatusIconWH, height: HMStatusIconWH))
        }
        
        // 名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(HMStatusCellMargin)
        }
        
        // 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(nameLabel)
        }
        
        // 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(HMStatusCellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        // 会员等级
        memberView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(HMStatusCellMargin)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        // 认证图标
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        
        // 微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(HMStatusCellMargin)
            make.top.equalTo(iconView.snp_bottom).offset(HMStatusCellMargin)
            make.trailing.equalTo(self).offset(-HMStatusCellMargin)
        }
        
        /// 微博配图视图
        picutreView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(HMStatusCellMargin)
            make.leading.equalTo(self).offset(HMStatusCellMargin)
//            make.trailing.equalTo(self).offset(-HMStatusCellMargin)
            
            // TODO: 测试高度先写死
//            make.height.equalTo(Int(arc4random_uniform(200)) + 100)
            make.size.equalTo(CGSizeZero)
        }
        
        /// 自定义的原创微博view的底部和contentLabel的底部重合,偏移8
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(picutreView).offset(HMStatusCellMargin)
        }
    }
    
    // MARK: - 懒加载
    /// 头像
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar"))
        
        // 设置阴影
//        imageView.layer.shadowColor = UIColor.greenColor().CGColor
//        imageView.layer.shadowOffset = CGSize(width: 4, height: 4)
//        imageView.layer.shadowOpacity = 1
//        imageView.layer.shadowRadius = 5
        
        // 设置
//        imageView.layer.cornerRadius = 17.5
//        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    /// 用户名称Label
    private lazy var nameLabel: UILabel = UILabel(fontSize: HMStatusNameFontSize, textColor: UIColor.darkGrayColor())
    
    /// 时间label
    private lazy var timeLabel: UILabel = UILabel(fontSize: HMStatusTimeFontSize, textColor: UIColor.orangeColor())
    
    /// 来源label
    private lazy var sourceLabel: UILabel = UILabel(fontSize: HMStatusTimeFontSize, textColor: UIColor.lightGrayColor())
    
    /// 会员图标
    private lazy var memberView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    
    /// 认证图标
    private lazy var verifiedView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    /// 原创微博内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.font = UIFont.systemFontOfSize(HMStatusContentFontSize)
        label.textColor = UIColor.darkGrayColor()
        
        let result = arc4random_uniform(100) % 2
        label.text = result == 0 ? "要下大雨了:要下大雨了:要下大雨了:要下大雨了:要下大雨了:要下大雨了:要下大雨了:要下大雨了:要下大雨了:要下大雨了:" : "明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨"
        label.sizeToFit()
        label.numberOfLines = 0
        
        // 换行宽度
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * HMStatusCellMargin
        
        return label
    }()
    
    /// 原创微博配图视图
    private lazy var picutreView: HMStatusPictureView = HMStatusPictureView()
}
